#include "qcolorproperty.h"

#include "../shared/xmlobject.h"

#include "../shared/qcommonstruct.h"

#include <QPainter>
#include <QGradient>
#include <QLinearGradient>

QColorProperty::QColorProperty(QAbstractProperty *parent):
    QAbstractProperty(parent)
{
    set_property("type","Color");
    QLinearGradient g;
    g.setColorAt(0,QColor(0,0,0));
    g.setColorAt(1,QColor(0,0,0));
    m_value.setValue<QGradient>(g);
}

QString QColorProperty::get_value_text()
{
    return "";
}

QIcon QColorProperty::get_value_icon()
{
    QPixmap pixmap=QPixmap(16,16);

    pixmap.fill(Qt::transparent);

    QPainter p(&pixmap);

    p.fillRect(QRect(0,0,16,16),QBrush(get_value().value<QGradient>()));

    return QIcon(pixmap);
}

void QColorProperty::toObject(XMLObject *xml)
{
    if(xml!=NULL)
    {
        xml->clear();
        xml->set_title(PROPERTY_TITLE);

        QMapIterator<QString,QVariant>      it(m_propertys);

        while(it.hasNext())
        {
            it.next();
            xml->set_property(it.key(),it.value().toString());
        }

        XMLObject *obj;

        QGradient g=get_value().value<QGradient>();
        QGradient::Type t=g.type();
        if(t==QGradient::LinearGradient)
        {
            const QLinearGradient *lg = static_cast<const QLinearGradient*>(&g);
            xml->set_property("x1",QString::number(lg->start().x()));
            xml->set_property("y1",QString::number(lg->start().y()));
            xml->set_property("x2",QString::number(lg->finalStop().x()));
            xml->set_property("y2",QString::number(lg->finalStop().y()));
            xml->set_property("gradient_type","qlineargradient");
        }
        else if(t==QGradient::RadialGradient)
        {
            const QRadialGradient *rg = static_cast<const QRadialGradient*>(&g);
            xml->set_property("cx",QString::number(rg->center().x()));
            xml->set_property("cy",QString::number(rg->center().y()));
            xml->set_property("radius",QString::number(rg->radius()));
            xml->set_property("fx",QString::number(rg->focalPoint().x()));
            xml->set_property("fy",QString::number(rg->focalPoint().y()));
            xml->set_property("gradient_type","qradialgradient");
        }
        else if(t==QGradient::ConicalGradient)
        {
            const QConicalGradient *cg = static_cast<const QConicalGradient*>(&g);
            xml->set_property("cx",QString::number(cg->center().x()));
            xml->set_property("cy",QString::number(cg->center().y()));
            xml->set_property("angle",QString::number(cg->angle()));
            xml->set_property("gradient_type","qconicalgradient");
        }

        xml->set_property("spread",QString::number(g.spread()));

        QVector<QPair<qreal, QColor> >stops=g.stops();

        for(int i=0;i<stops.size();i++)
        {
            QPair<qreal,QColor> stop=stops.at(i);
            obj=new XMLObject(xml);
            obj->set_title("Color");
            obj->set_property("Red",QString::number(stop.second.red()));
            obj->set_property("Green",QString::number(stop.second.green()));
            obj->set_property("Blue",QString::number(stop.second.blue()));
            obj->set_property("Alpha",QString::number(stop.second.alpha()));
            obj->set_property("Stop",QString::number(stop.first));
        }
    }
}

void QColorProperty::fromObject(XMLObject *xml)
{
    if(xml!=NULL)
    {
        if(xml->get_title()!=PROPERTY_TITLE)
        {
            return;
        }


        QMapIterator<QString,QString>      it(xml->get_propertys());

        while(it.hasNext())
        {
            it.next();
            m_propertys.insert(it.key(),it.value());
        }

        QGradient g;

        QString type=m_propertys.value("gradient_type","qlineargradient").toString();
        if(type=="qlineargradient")
        {
            qreal x1=m_propertys.value("x1","0").toDouble();
            qreal y1=m_propertys.value("y1","0").toDouble();
            qreal x2=m_propertys.value("x2","0").toDouble();
            qreal y2=m_propertys.value("y2","1").toDouble();
            g=QLinearGradient(x1,y1,x2,y2);
        }
        else if(type=="qradialgradient")
        {
            qreal cx=m_propertys.value("cx","0.5").toDouble();
            qreal cy=m_propertys.value("cy","0.5").toDouble();
            qreal radius=m_propertys.value("radius","0.5").toDouble();
            qreal fx=m_propertys.value("fx","0.5").toDouble();
            qreal fy=m_propertys.value("fy","0.5").toDouble();
            g=QRadialGradient(cx,cy,radius,fx,fy);
        }
        else if(type=="qconicalgradient")
        {
            qreal cx=m_propertys.value("cx","0.5").toDouble();
            qreal cy=m_propertys.value("cy","0.5").toDouble();
            qreal angle=m_propertys.value("angle","0").toDouble();
            g=QConicalGradient(cx,cy,angle);
        }

        g.setSpread((QGradient::Spread)m_propertys.value("spread",0).toInt());

        QColor color;
        foreach(XMLObject* obj,xml->get_children())
        {
            color=QColor(obj->get_property("Red").toInt()&0xff,
                         obj->get_property("Green").toInt()&0xff,
                         obj->get_property("Blue").toInt()&0xff,
                         obj->get_property("Alpha").toInt()&0xff);
            g.setColorAt(obj->get_property("Stop").toDouble(),color);
        }
        g.setCoordinateMode(QGradient::StretchToDeviceMode);
        m_value.setValue<QGradient>(g);
        refresh();
    }
}

qRegisterProperty("Color",QColorProperty)
